<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-10-09
 * Time: 15:25:54
 * Info:
 */

namespace App\api\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

class AuthCheckAccess implements MiddlewareInterface
{

    public function process(Request $request, callable $handler): Response
    {
        //配置统一入口，只让访问index方法
        $controllerName = explode('\\', request()->controller);
        if (request()->app != 'api' || end($controllerName) != 'IndexController' || request()->action != 'index') {
            if ( ! (request()->app == 'api' && end($controllerName) == 'CommonController')) {
                //这个if是为了兼容api/Common控制器可以直接访问，为了向下兼容
                return response('error');
            }
        }

        return $handler($request);
    }

}
